package club.beimeng.service.impl;

import club.beimeng.config.ConifgEntity;
import club.beimeng.service.VideoService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
import java.util.logging.Logger;

@Service
public class VideoServiceImpl implements VideoService {
    private final static Logger logger = Logger.getLogger("SaveVideo");

    @Override
    public String uploadVideo(MultipartFile videoFile) {
        logger.info("开始 uploadVideo");
        if (videoFile == null){
            logger.warning("传入videoFile为空");
            return null;
        }

        // 获取原有名称，使用UUID设置新名称，根据时间仓库设置保存路径路径
        String originalFilename = videoFile.getOriginalFilename().replace(" ","");
        String fileName = UUID.randomUUID().toString().replaceAll("-","") + originalFilename ;

        // 根据日期进行分类
        String datePath = new SimpleDateFormat("yy/MM/dd/").format(new Date());
        String savePath = ConifgEntity.Repo_Path + datePath + fileName;

        //判断分类文件夹是否存在，不存在则创建
        File file = new File(ConifgEntity.Repo_Path + datePath);
        if (!file.exists()) {
            file.mkdirs();
        }

        // 设置输出流，写数据
        try {
            videoFile.transferTo(new File(savePath));
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 设置出访问路径，return
        String answer = ConifgEntity.Nginx_Path + datePath + fileName;
        logger.info(fileName + "上传成功");
        logger.info("视频保存位置  :  " + savePath);
        logger.info( "网络访问位置  :  " + answer);
        return answer;
    }

    @Override
    public Boolean removeVideo(String sourceVideoUrl) {
        logger.info("开始 removeVideo");
        logger.info("get  :  " + sourceVideoUrl);
        //找到想要逻辑删除的文件地址
        String filePath = sourceVideoUrl.replace(ConifgEntity.Nginx_Path,"");
        String realFilePath = ConifgEntity.Repo_Path + filePath;
        //定义回收站地址
        String garbagePath = ConifgEntity.Repo_Path + "garbage/" ;
        String garbageFilePath = ConifgEntity.Repo_Path + "garbage/"+ filePath.replace("/","-");
        File file = new File(garbagePath);
        if (!file.exists()) {
            file.mkdirs();
        }
        //将文件剪切到回收站地址
        try {
            FileInputStream fileInputStream = new FileInputStream(realFilePath);
            FileOutputStream outputStream = new FileOutputStream(garbageFilePath);

            byte[] bytes = new byte[1024];
            int len = 0;
            while ((len = fileInputStream.read(bytes)) != -1){
                outputStream.write(bytes,0 ,len);
            }
            outputStream.close();
            fileInputStream.close();
            File deleteFile = new File(realFilePath);
            if (deleteFile.delete()) {
                logger.info("文件删除成功(文件已经被移动到👇↓)");
                logger.info(garbageFilePath);
            }
        }catch (Exception e){
            logger.warning(e.toString());
            return false;
        }
        return true;
    }
}
/*
 *
 * @author Mr.Bao
 * @date 2021/7/30 20:56
 */